package com.example.administrator.z;

import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import com.example.administrator.z.fragment.HomePagerFragment;
import com.example.administrator.z.fragment.MeFragment;
import com.example.administrator.z.fragment.MoralEducationFragment;
import com.example.administrator.z.fragment.SecurityFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<String>mList;
    private List<Fragment>mFragment;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
        initData();
        initView();

    }

    private void initData() {
        mList = new ArrayList<>();
        mList.add("首页");
        mList.add("德育生活");
        mList.add("安全呵护");
        mList.add("我的");
        mFragment = new ArrayList<>();
        mFragment.add(new HomePagerFragment());
        mFragment.add(new MeFragment());
        mFragment.add(new MoralEducationFragment());
        mFragment.add(new SecurityFragment());



    }

    private void initView() {
        mTabLayout =(TabLayout) findViewById(R.id.mTabLayout);
        mViewPager = (ViewPager) findViewById(R.id.mViewPager);
        mViewPager.setOffscreenPageLimit(mFragment.size());
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragment.get(position);
            }

            @Override
            public int getCount() {
                return mFragment.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return mList.get(position);
            }
        });

        mTabLayout.setupWithViewPager(mViewPager);


    }
}
